package com.gugu.procedure.controller;


import com.gugu.procedure.bean.InsideOrder;
import com.gugu.procedure.bean.OrderItem;
import com.gugu.procedure.common.Constant;
import com.gugu.procedure.common.MessageResult;
import com.gugu.procedure.dao.OrderItemDao;
import com.gugu.procedure.param.*;
import com.gugu.procedure.service.InsideOrderService;
import com.gugu.procedure.util.BeanUtil;
import com.gugu.procedure.util.DateUtil;
import com.gugu.procedure.util.NumberUtil;
import com.gugu.procedure.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

@RestController
@Api(tags = {"内部订单模块"})
@RequestMapping("insideOrder")
public class InsideOrderController extends BaseController {

    @Autowired
    private InsideOrderService insideOrderService;
    @Autowired
    private OrderItemDao orderItemDao;


    @PostMapping("add")
    @ApiOperation(value = "新增订单")

    public MessageResult add(@RequestBody InsideOrderAddParam insideOrderAddParam) {
        return insideOrderService.add(insideOrderAddParam);
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public MessageResult page(String orderNo, String custOrderNo, String custCompany, String customer, String param, String startTime, String endTime, String status, int pageNo, int pageSize) {
        return insideOrderService.page(getRole(), orderNo, custOrderNo, custCompany, customer, param, startTime, endTime, status, pageNo, pageSize);
    }

    @GetMapping("insideOrderDetail")
    @ApiOperation(value = "订单详情")
    public MessageResult page(String orderNo, String item) {
        return insideOrderService.insideOrderDetail(orderNo, item);
    }

    @GetMapping("open/test")
    @ApiOperation(value = "根据订单和规格查询")
    public void test(String orderNo) {
        insideOrderService.initFirstProduction(orderNo);
    }


    @GetMapping("open/orderNoAndSpecs")
    @ApiOperation(value = "根据订单和规格查询")
    public MessageResult findTopByOrderNoAndSpecs(String orderNo, String specs) {
        return MessageResult.getSuccessInstance(insideOrderService.findTopByOrderNoAndSpecs(orderNo, specs));
    }


    @GetMapping("open/orderNoAndNumber")
    @ApiOperation(value = "根据订单和模具编号查询")
    public MessageResult queryByOrderNoAndNumber(String orderNo, String number) {
        return MessageResult.getSuccessInstance(insideOrderService.queryByOrderNoAndNumber(orderNo, number));
    }

    @GetMapping("open/parameterUpAndDown")
    @ApiOperation(value = "")
    public MessageResult parameterUpAndDown(String orderNo, String specs, String production) {
        return MessageResult.getSuccessInstance(insideOrderService.queryParams(orderNo, specs, production));
    }


    @GetMapping("export")
    @ApiOperation(value = "订单导出")
    public void export(String orderNo) {

    }


    @GetMapping("remove")
    @ApiOperation(value = "删除订单")
    public MessageResult page(String orderNo) {
        if (!checkRole(Constant.MANAGER_ROLE)) {
            return MessageResult.error(Constant.PERMISSIONS_ERROR);
        }
        return insideOrderService.remove(orderNo);
    }

    @PostMapping("check")
    @ApiOperation(value = "订单审核")
    public MessageResult checkOrder(@RequestBody InsideOrderCheckParam insideOrderCheckParam) {
        return insideOrderService.checkOrder(getRole(), getAccount(), getName(), insideOrderCheckParam);
    }

    @PostMapping("paramRetry")
    @ApiOperation(value = "参数重导")
    public MessageResult paramRetry(@RequestBody InsideOrderCheckParam insideOrderCheckParam) {
        return insideOrderService.paramRetry(getAccount(), insideOrderCheckParam);
    }


    @PostMapping("updateForBusiness")
    @ApiOperation(value = "业务课更新")
    @Transactional
    public MessageResult updateForBusiness(@RequestBody InsideOrderBusinessUpdate insideOrderBusinessUpdate) {
        // 权限判断
        if (!checkRole(Constant.MANAGER_BUSINESS)) {
            return MessageResult.error(Constant.PERMISSIONS_ERROR);
        }
        InsideOrder insideOrder = insideOrderService.queryInsideOrderByOrderNo(insideOrderBusinessUpdate.getOrderNo());
        if (Objects.isNull(insideOrder)) {
            return MessageResult.error("订单不存在");
        }
        OrderItem orderItem = insideOrderService.findByOrderNoAndSeque(insideOrderBusinessUpdate.getOrderNo(), insideOrderBusinessUpdate.getSeque());
        if (Objects.isNull(orderItem)) {
            return MessageResult.error(Constant.DATA_ERROR);
        }
        BeanUtil.copyProperties(insideOrderBusinessUpdate, orderItem);
        insideOrderService.updateOrderItem(orderItem);
        insideOrder.setBusiness(getName());
        insideOrder.setProductionDate(DateUtil.getFormatedDate());
        insideOrderService.updateInsideOrder(insideOrder);
        return MessageResult.success();
    }


    @PostMapping("updateForTechnology")
    @ApiOperation(value = "技术课更新")
    @Transactional
    public MessageResult updateForBusiness(@RequestBody InsideOrderTechnologyUpdate insideOrderTechnologyUpdate) {
        // 权限判断
        if (!checkRole(Constant.MANAGER_TECHNOLOGY)) {
            return MessageResult.error(Constant.PERMISSIONS_ERROR);
        }
        InsideOrder insideOrder = insideOrderService.queryInsideOrderByOrderNo(insideOrderTechnologyUpdate.getOrderNo());
        if (Objects.isNull(insideOrder)) {
            return MessageResult.error("订单不存在");
        }
        OrderItem orderItem = insideOrderService.findByOrderNoAndSeque(insideOrderTechnologyUpdate.getOrderNo(), insideOrderTechnologyUpdate.getSeque());
        if (Objects.isNull(orderItem)) {
            return MessageResult.error(Constant.DATA_ERROR);
        }
        BeanUtil.copyProperties(insideOrderTechnologyUpdate, orderItem);
        insideOrderService.updateOrderItem(orderItem);
        insideOrder.setTechnology(getName());
        insideOrder.setTechnologyDate(DateUtil.getFormatedDate());
        insideOrderService.updateInsideOrder(insideOrder);
        return MessageResult.success();
    }

    @PostMapping("updateForQc")
    @ApiOperation(value = "品管课更新")
    @Transactional
    public MessageResult updateForBusiness(@RequestBody InsideOrderQcUpdate insideOrderQcUpdate) {
        // 权限判断
        if (!checkRole(Constant.MANAGER_QC)) {
            return MessageResult.error(Constant.PERMISSIONS_ERROR);
        }
        InsideOrder insideOrder = insideOrderService.queryInsideOrderByOrderNo(insideOrderQcUpdate.getOrderNo());
        if (Objects.isNull(insideOrder)) {
            return MessageResult.error("订单不存在");
        }
        OrderItem orderItem = insideOrderService.findByOrderNoAndSeque(insideOrderQcUpdate.getOrderNo(), insideOrderQcUpdate.getSeque());
        if (Objects.isNull(orderItem)) {
            return MessageResult.error(Constant.DATA_ERROR);
        }
        BeanUtil.copyProperties(insideOrderQcUpdate, orderItem);
        insideOrderService.updateOrderItem(orderItem);
        insideOrder.setQc(getName());
        insideOrder.setQcDate(DateUtil.getFormatedDate());
        insideOrderService.updateInsideOrder(insideOrder);
        return MessageResult.success();
    }

    @PostMapping("updateForPlain")
    @ApiOperation(value = "计划课更新")
    @Transactional
    public MessageResult updateForBusiness(@RequestBody InsideOrderPlanUpdate insideOrderPlanUpdate) {
        // 权限判断
        if (!checkRole(Constant.MANAGER_PLAIN)) {
            return MessageResult.error(Constant.PERMISSIONS_ERROR);
        }
        InsideOrder insideOrder = insideOrderService.queryInsideOrderByOrderNo(insideOrderPlanUpdate.getOrderNo());
        if (Objects.isNull(insideOrder)) {
            return MessageResult.error("订单不存在");
        }
        if (StringUtil.notEquals(insideOrderPlanUpdate.getReplyDate(), insideOrder.getReadyDate())) {
            insideOrder.setReplyDate(insideOrderPlanUpdate.getReplyDate());
            insideOrder.setPlan(getName());
            insideOrder.setPlanDate(DateUtil.getFormatedDate());
            insideOrderService.updateInsideOrder(insideOrder);
        }
        return MessageResult.success();
    }

    @PostMapping("updateForRequirement2")
    @ApiOperation(value = "内部特殊要求更新")
    @Transactional
    public MessageResult updateForBusiness(@RequestBody UpdateForRequirement updateForRequirement) {
        // 权限判断
        if (!checkRole(Constant.MANAGER_QC)) {
            return MessageResult.error(Constant.PERMISSIONS_ERROR);
        }
        InsideOrder insideOrder = insideOrderService.queryInsideOrderByOrderNo(updateForRequirement.getOrderNo());
        if (Objects.isNull(insideOrder)) {
            return MessageResult.error("订单不存在");
        }
        if (StringUtil.notEquals(updateForRequirement.getRequirement2(), insideOrder.getRequirement2())) {
            insideOrder.setRequirement2(updateForRequirement.getRequirement2());
            insideOrder.setQc(getName());
            insideOrder.setQcDate(DateUtil.getFormatedDate());
            insideOrderService.updateInsideOrder(insideOrder);
        }
        return MessageResult.success();
    }

    @PostMapping("updateOrderItem")
    @ApiOperation(value = "更新")
    public MessageResult update(@RequestBody List <OrderItem> list) {
        //如果第一个numberStart 更新变化了 则整个集合的模具编码都需要重新生成
        list.stream().sorted(Comparator.comparing(OrderItem::getSeque));
        OrderItem orderItem = list.get(0);
        if (orderItem.getId() != null) {
            OrderItem orderItem1 = orderItemDao.findById(orderItem.getId()).orElse(null);
            // 判断有没有修改startNumber
            if (!orderItem.getNumberStart().equals(orderItem1.getNumberStart())) {
                String startNumber = orderItem.getNumberStart();
                for (OrderItem item : list) {
                    item.setNumberStart(startNumber);
                    int amount = Integer.parseInt(item.getAmount());
                    String endNumber = startNumber;
                    for (int i = 1; i < amount; i++) {
                        endNumber = NumberUtil.nextNumber(endNumber);
                    }
                    item.setNumberEnd(endNumber);
                    startNumber = NumberUtil.nextNumber(endNumber);
                }
            }
        }
        insideOrderService.updateOrderItemList(list);
        return MessageResult.success();
    }
}
